import 'package:flutter/material.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:sidebarx/sidebarx.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:get/get.dart';
import '../../constants.dart';
import '../../controller/cache.dart';
import '../../pages/book/bookForApp.dart';

class TeacherSidebarX extends StatefulWidget {
  const TeacherSidebarX(
      {Key? key, required this.selectedIndex, required this.isBigScreen})
      : super(key: key);
  final int selectedIndex;
  final bool isBigScreen;

  @override
  State<TeacherSidebarX> createState() => _TeacherSidebarXState();
}

class _TeacherSidebarXState extends State<TeacherSidebarX> {
  final SidebarXController controller =
      SidebarXController(selectedIndex: 0, extended: false);
  final PageStyles _pageStyles = PageStyles();

  @override
  void initState() {
    super.initState();

    controller.selectIndex(widget.selectedIndex);

    if (widget.isBigScreen) {
      controller.setExtended(true);
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('start building Teacher SidebarX');

    return Container(
      margin: _pageStyles.sideBarPageEdgeInsets(),
      //decoration: _pageStyles.pageSidebarDecoration(),
      decoration: BoxDecoration(
        color: Get.isDarkMode
            ? Colors.white.withOpacity(0.1)
            : Get.theme.primaryColor.withOpacity(0.9),
        borderRadius: BorderRadius.circular(6.0),
      ),
      child: SidebarX(
        controller: controller,
        theme: _pageStyles.pageSidebarXTheme(),
        extendedTheme: _pageStyles.pageSidebarXExtendTheme(),
        //footerDivider: divider,
        headerBuilder: (context, extended) {
          return Container(
            child: Container(
              margin: EdgeInsets.symmetric(vertical: 3.0),
              padding: EdgeInsets.all(6.0),
              decoration: BoxDecoration(
                  color: Colors.black.withOpacity(0.1), shape: BoxShape.circle),
              child: GestureDetector(
                  onTap: () {
                    Get.offAllNamed('/teacher/home');
                  },
                  child: Image.asset('assets/images/profile6.png')),
            ),
          );
        },
        //lists 之间没有调整间距的选项
        items: RoutePages.teacherPageItems
            .map((page) => SidebarXItem(
                  iconWidget: Tooltip(
                    textStyle: TextStyle(
                      fontSize: 18.0,
                        color: Get.isDarkMode
                            ? Get.theme.primaryColor
                            : Colors.white),
                    message: page.name.tr,
                    child: Icon(page.icon,
                        size: 27.0,
                        color: Get.isDarkMode
                            ? Get.theme.primaryColorDark
                            : Colors.white),
                  ),
                  // icon: page.icon,
                  label: page.name.tr,
                  onTap: () {
                    if (page.name == 'Core Materials') {
                      String programId = box.get(
                          CacheManagerKey['currentclassProgramId'].toString());
                      var program = Constants.nanhaiImmersionSubProgramsDropdown
                          .where((element) => element.value == programId)
                          .toList();
                      if (GetPlatform.isWeb) {
                        if (program.isNotEmpty) {
                          Get.toNamed('/teacher/corematerials');
                        } else {
                          Get.toNamed('/teacher/themedbooks');
                        }
                      } else {
                        if (program.isNotEmpty) {
                          Get.offAllNamed('/teacher/corematerials');
                        } else {
                          Get.offAllNamed('/teacher/themedbooks');
                        }
                      }
                    } else if (page.name == 'Home') {
                      Get.offAllNamed('/teacher/home');
                    } else {
                      if (GetPlatform.isWeb) {
                        Get.toNamed(page.route);
                        // if (Get.currentRoute == '/teacher/library') {
                        //   Get.toNamed(page.route);
                        // } else {
                        //   Get.toNamed(page.route);
                        // }
                      } else {
                        Get.offAllNamed(page.route);
                      }
                    }
                  },
                ))
            .toList(),
      ),
    );
  }
}
